import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _class, _class2;

/**
 * userMessage 站内消息汇总
 * @date: 2018-8-4

 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import { Form, Spin, Tabs } from 'antd';
import { Bind } from 'lodash-decorators';
import { Content, Header } from 'components/Page';
import CacheComponent from 'components/CacheComponent';
import { Button as ButtonPermission } from 'components/Permission';
import formatterCollections from 'utils/intl/formatterCollections';
import { getCurrentOrganizationId } from 'utils/utils';
import intl from 'utils/intl';
import notification from 'utils/notification';
import MessageTabPane from "./MessageTabPane";
/**
 * 站内消息
 * @extends {Component} - PureComponent
 * @reactProps {Object} [match={}] - react-router match路由信息
 * @reactProps {Object} [history={}]
 * @reactProps {Object} userMessage - 数据源
 * @reactProps {loading} loading - 数据加载是否完成
 * @reactProps {Function} [dispatch=function(e) {return e;}] - redux dispatch方法
 * @return React.element
 */

var UserMessage = (_dec = connect(function (_ref) {
  var userMessage = _ref.userMessage,
      loading = _ref.loading;
  return {
    userMessage: userMessage,
    loading: loading.effects['userMessage/queryMessage'],
    deleting: loading.effects['userMessage/deleteMessage'],
    changeReading: loading.effects['userMessage/changeRead'],
    organizationId: getCurrentOrganizationId()
  };
}), _dec2 = Form.create({
  fieldNameProp: null
}), _dec3 = formatterCollections({
  code: ['cmsg.userMessage', 'cmsg.common']
}), _dec4 = CacheComponent({
  cacheKey: '/cmsg/userMessage'
}), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec(_class = _dec2(_class = _dec3(_class = _dec4(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(UserMessage, _PureComponent);

  var _super = _createSuper(UserMessage);

  function UserMessage(props) {
    var _this;

    _classCallCheck(this, UserMessage);

    _this = _super.call(this, props);
    _this.state = {};
    _this.filterFormRef = React.createRef();
    _this.messageTabPaneRef = React.createRef();
    _this.noticeTabPaneRef = React.createRef();
    _this.announceTabPaneRef = React.createRef();
    return _this;
  }
  /**
   * 按条件查询
   */


  _createClass(UserMessage, [{
    key: "handleSearchMessage",
    value: function handleSearchMessage(payload, type) {
      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          organizationId = _this$props.organizationId;
      return dispatch({
        type: 'userMessage/queryMessage',
        payload: _objectSpread(_objectSpread({}, payload), {}, {
          organizationId: organizationId,
          type: type
        })
      });
    }
    /**
     * 跳转到详情界面
     * @param {object} record
     * @param {'message' | 'notice' | 'announce'} type
     */

  }, {
    key: "handleDetails",
    value: function handleDetails(record, type) {
      var dispatch = this.props.dispatch;
      var gotoUrl = type === 'announce' ? "/cmsg/user-message/detail/".concat(type, "/").concat(record.noticeId) : "/cmsg/user-message/detail/".concat(type, "/").concat(record.userMessageId);
      dispatch(routerRedux.push(gotoUrl));
    }
    /**
     * 标记已读
     * @param {*} number
     */

  }, {
    key: "handleRead",
    value: function handleRead(number) {
      var _this2 = this;

      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          organizationId = _this$props2.organizationId;
      var updatePromise;

      if (number) {
        updatePromise = dispatch({
          type: 'userMessage/changeRead',
          payload: {
            readAll: 1,
            organizationId: organizationId
          }
        });
      } else {
        var selectedRows = this.getCurrentSelectedRows();
        var userMessageId = selectedRows.map(function (item) {
          return item.userMessageId;
        });
        var userMessageIdList = // eslint-disable-next-line no-nested-ternary
        userMessageId.length > 0 ? userMessageId.join(',') : userMessageId.length !== 0 ? userMessageId[0] : [];
        updatePromise = dispatch({
          type: 'userMessage/changeRead',
          payload: {
            userMessageIdList: userMessageIdList,
            organizationId: organizationId
          }
        });
      }

      updatePromise.then(function (res) {
        if (res) {
          notification.success();
          var _this2$props$userMess = _this2.props.userMessage.currentType,
              currentType = _this2$props$userMess === void 0 ? 'message' : _this2$props$userMess;
          var _this2$props$userMess2 = _this2.props.userMessage[currentType].pagination,
              pagination = _this2$props$userMess2 === void 0 ? {} : _this2$props$userMess2;

          var readFlag = _this2.getReadFlag();

          _this2.handleSearchMessage({
            page: pagination,
            readFlag: readFlag
          }, currentType);
        }
      });
    }
    /**
     * 删除消息
     */

  }, {
    key: "handleDelete",
    value: function handleDelete() {
      var _this3 = this;

      var _this$props3 = this.props,
          dispatch = _this$props3.dispatch,
          organizationId = _this$props3.organizationId;
      var selectedRows = this.getCurrentSelectedRows();
      var userMessageId = selectedRows.map(function (item) {
        return item.userMessageId;
      });
      var userMessageIdList = // eslint-disable-next-line no-nested-ternary
      userMessageId.length > 0 ? userMessageId.join(',') : userMessageId.length !== 0 ? userMessageId[0] : [];
      dispatch({
        type: 'userMessage/deleteMessage',
        payload: {
          userMessageIdList: userMessageIdList,
          organizationId: organizationId
        }
      }).then(function (res) {
        if (res) {
          notification.success();
          var _this3$props$userMess = _this3.props.userMessage.currentType,
              currentType = _this3$props$userMess === void 0 ? 'message' : _this3$props$userMess;
          var _this3$props$userMess2 = _this3.props.userMessage[currentType].pagination,
              pagination = _this3$props$userMess2 === void 0 ? {} : _this3$props$userMess2;

          var readFlag = _this3.getReadFlag();

          _this3.handleSearchMessage({
            page: pagination,
            readFlag: readFlag
          }, currentType);
        }
      });
    } // 计算信息

  }, {
    key: "getCurrentSelectedRows",
    value: function getCurrentSelectedRows() {
      var _this$props$userMessa = this.props.userMessage.currentType,
          currentType = _this$props$userMessa === void 0 ? 'message' : _this$props$userMessa;
      var current;

      switch (currentType) {
        case 'message':
          current = this.messageTabPaneRef.current;
          break;

        case 'notice':
          current = this.noticeTabPaneRef.current;
          break;

        case 'announce':
          current = this.announceTabPaneRef.current;
          break;

        default:
          break;
      }

      if (current) {
        var _current$state$select = current.state.selectedRows,
            selectedRows = _current$state$select === void 0 ? [] : _current$state$select;
        return selectedRows;
      } else {
        return [];
      }
    } // 获取 当前 tab 的组件中的 readFlag 状态

  }, {
    key: "getReadFlag",
    value: function getReadFlag() {
      var _this$props$userMessa2 = this.props.userMessage.currentType,
          currentType = _this$props$userMessa2 === void 0 ? 'message' : _this$props$userMessa2;
      var current;

      switch (currentType) {
        case 'message':
          current = this.messageTabPaneRef.current;
          break;

        case 'notice':
          current = this.noticeTabPaneRef.current;
          break;

        case 'announce':
          current = this.announceTabPaneRef.current;
          break;

        default:
          break;
      }

      if (current) {
        var readTypeValue = current.state.readTypeValue;

        switch (readTypeValue) {
          case '0':
            return 0;

          case '1':
            return 1;

          case 'all':
          default:
            return undefined;
        }
      } else {
        return undefined;
      }
    } // 强制刷新index

  }, {
    key: "callForceUpdate",
    value: function callForceUpdate() {
      this.forceUpdate();
    } // Tabs

  }, {
    key: "tabChange",
    value: function tabChange(tabKey) {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'userMessage/updateState',
        payload: {
          currentType: tabKey
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this4 = this;

      var _this$props4 = this.props,
          _this$props4$userMess = _this$props4.userMessage,
          _this$props4$userMess2 = _this$props4$userMess.message,
          message = _this$props4$userMess2 === void 0 ? {} : _this$props4$userMess2,
          _this$props4$userMess3 = _this$props4$userMess.notice,
          notice = _this$props4$userMess3 === void 0 ? {} : _this$props4$userMess3,
          _this$props4$userMess4 = _this$props4$userMess.announce,
          announce = _this$props4$userMess4 === void 0 ? {} : _this$props4$userMess4,
          _this$props4$userMess5 = _this$props4$userMess.currentType,
          currentType = _this$props4$userMess5 === void 0 ? 'message' : _this$props4$userMess5,
          _this$props4$loading = _this$props4.loading,
          loading = _this$props4$loading === void 0 ? false : _this$props4$loading,
          _this$props4$deleting = _this$props4.deleting,
          deleting = _this$props4$deleting === void 0 ? false : _this$props4$deleting,
          _this$props4$changeRe = _this$props4.changeReading,
          changeReading = _this$props4$changeRe === void 0 ? false : _this$props4$changeRe,
          path = _this$props4.match.path;

      var messageProps = _objectSpread(_objectSpread({}, message), {}, {
        fetchMessage: this.handleSearchMessage,
        onGotoDetail: this.handleDetails,
        indexForceUpdate: this.callForceUpdate
      });

      var noticeProps = _objectSpread(_objectSpread({}, notice), {}, {
        fetchMessage: this.handleSearchMessage,
        onGotoDetail: this.handleDetails,
        indexForceUpdate: this.callForceUpdate
      });

      var announceProps = _objectSpread(_objectSpread({}, announce), {}, {
        fetchMessage: this.handleSearchMessage,
        onGotoDetail: this.handleDetails // 公告 不能操作
        // indexForceUpdate: this.callForceUpdate,

      });

      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cmsg.userMessage.view.message.title').d('站内消息')
      }, currentType !== 'announce' && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.delete"),
          type: 'text',
          meaning: '站内消息-删除'
        }],
        type: "primary",
        icon: "delete",
        loading: deleting,
        onClick: this.handleDelete,
        disabled: changeReading || loading || _isEmpty(this.getCurrentSelectedRows())
      }, intl.get('cpaas.common.button.delete').d('删除')), /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.signRead"),
          type: 'button',
          meaning: '站内消息-标记已读'
        }],
        icon: "mail",
        onClick: function onClick() {
          _this4.handleRead();
        },
        loading: changeReading,
        disabled: deleting || loading || _isEmpty(this.getCurrentSelectedRows().filter(function (record) {
          return record.readFlag !== 1;
        }))
      }, intl.get('cmsg.userMessage.view.option.signRead').d('标记已读')), /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.addRead"),
          type: 'button',
          meaning: '站内消息-全部已读'
        }],
        icon: "mail",
        disabled: deleting || loading,
        loading: changeReading,
        onClick: function onClick() {
          _this4.handleRead(1);
        }
      }, intl.get('cmsg.userMessage.view.option.allRead').d('全部已读')))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(Spin, {
        spinning: loading
      }, /*#__PURE__*/React.createElement(Tabs, {
        activeKey: currentType,
        onChange: this.tabChange,
        animated: false
      }, /*#__PURE__*/React.createElement(Tabs.TabPane, {
        tab: intl.get('cmsg.userMessage.view.title.message').d('消息'),
        key: "message"
      }, /*#__PURE__*/React.createElement(MessageTabPane, Object.assign({}, messageProps, {
        type: "message",
        ref: this.messageTabPaneRef
      }))), /*#__PURE__*/React.createElement(Tabs.TabPane, {
        tab: intl.get('cmsg.userMessage.view.title.notice').d('通知'),
        key: "notice"
      }, /*#__PURE__*/React.createElement(MessageTabPane, Object.assign({}, noticeProps, {
        type: "notice",
        ref: this.noticeTabPaneRef
      }))), /*#__PURE__*/React.createElement(Tabs.TabPane, {
        tab: intl.get('cmsg.userMessage.view.title.announce').d('公告'),
        key: "announce"
      }, /*#__PURE__*/React.createElement(MessageTabPane, Object.assign({}, announceProps, {
        type: "announce",
        ref: this.announceTabPaneRef
      })))))));
    }
  }]);

  return UserMessage;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "handleSearchMessage", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearchMessage"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDetails", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDetails"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRead", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRead"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDelete", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDelete"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "getCurrentSelectedRows", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "getCurrentSelectedRows"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "getReadFlag", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "getReadFlag"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "callForceUpdate", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "callForceUpdate"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "tabChange", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "tabChange"), _class2.prototype)), _class2)) || _class) || _class) || _class) || _class);
export { UserMessage as default };